#include "menu.h"

#include "gamestatemanager.h"

Menu::Menu(GameStateManager& gsm, GameInfo& gameInfo) : _gui(*new GuiSystem()),
    _gameInfo(gameInfo), _view(*new MenuView(*this, _gui)), _gsm(gsm)
{
    //ctor
}

Menu::~Menu()
{
    //dtor
}

bool Menu::update(float dt, const Input& input)
{
    GuiId element;
    int selected;
    GuiEvent event = _gui.interact(input, &element, &selected);

    if (event == GE_CLICKED && element == GUI_START) {

        // Start game

        _gsm.pushState(GST_GAME);
        return false;

    } else if (event == GE_ITEMSELECTED) {
        if (element == GUI_BLACKSELECT) {
            if (selected == 0) {
                _gameInfo.setBlack(HUMAN);
            } else {
                _gameInfo.setBlack(AI);
            }
        } else if (element == GUI_WHITESELECT) {
            if (selected == 0) {
                _gameInfo.setWhite(HUMAN);
            } else {
                _gameInfo.setWhite(AI);
            }
        } else if (element == GUI_AISELECT) {
            if (selected == 0) {
                _gameInfo.setAiDepth(2);
            } else if (selected == 1) {
                _gameInfo.setAiDepth(4);
            } else if (selected == 2) {
                _gameInfo.setAiDepth(5);
            }
        }
    }

    return true;
}

IGameStateView& Menu::getView()
{
    return _view;
}

void Menu::onEnter()
{

}

MenuView::MenuView(Menu& model, GuiSystem& gui) : _model(model), _gui(gui)
{
    _gui.addStatic(0, 0.0f, 0.0f, 1.0f, 1.0f,
        *new Texture("..\\..\\models\\textures\\background.png"));

    _gui.addStatic(1, 0.25f, 0.8f, _gui.widthToRel(512), _gui.heightToRel(128),
        *new Texture("..\\..\\models\\textures\\logo_big.png", true));

    _gui.addStatic(2, 0.4f, 0.5f, _gui.widthToRel(256), _gui.heightToRel(64),
        *new Texture("..\\..\\models\\textures\\text_black.png", true));

    _gui.addSelectBox(GUI_BLACKSELECT, 0.4f, 0.3f,
        _gui.widthToRel(256), _gui.heightToRel(128),
        *new Texture("..\\..\\models\\textures\\text_humanai.png", true), 2, 1);

    _gui.addStatic(3, 0.1f, 0.5f, _gui.widthToRel(256), _gui.heightToRel(64),
        *new Texture("..\\..\\models\\textures\\text_white.png", true));

    _gui.addSelectBox(GUI_WHITESELECT, 0.1f, 0.3f,
        _gui.widthToRel(256), _gui.heightToRel(128),
        *new Texture("..\\..\\models\\textures\\text_humanai.png", true), 2);

    _gui.addStatic(4, 0.7f, 0.5f, _gui.widthToRel(256), _gui.heightToRel(64),
        *new Texture("..\\..\\models\\textures\\text_ailevel.png", true));

    _gui.addSelectBox(GUI_AISELECT, 0.7f, 0.212f,
        _gui.widthToRel(256), _gui.heightToRel(256),
        *new Texture("..\\..\\models\\textures\\text_ailevels.png", true),
        3, 1, _gui.heightToRel(195));

    _gui.addButton(GUI_START, 0.4f, 0.1f,
        _gui.widthToRel(256), _gui.heightToRel(64),
        *new Texture("..\\..\\models\\textures\\text_start.png", true));

}

void MenuView::draw(bool clear)
{
    _gui.draw();
}

void MenuView::onEnter()
{

}
